//
//  PlanCell.m
//  SportSocial
//
//  Created by yull on 15/2/3.
//  Copyright (c) 2015年 yull. All rights reserved.
//

#import "PlanRunCell.h"

@implementation PlanRunCell

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        CGRect rect = CGRectMake(15, 0, self.contentView.width - 30, self.contentView.height);
        UIView *backView = [[UIView alloc] initWithFrame:rect];
        backView.backgroundColor = RGB(255, 247, 239);
        backView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        [self.contentView addSubview:backView];
        
        rect = CGRectMake(0, backView.height - 1, backView.width, 1);
        UIView *lineView = [[UIView alloc] initWithFrame:rect];
        lineView.backgroundColor = RGB(248, 238, 225);
        lineView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
        [backView addSubview:lineView];
        
        rect = CGRectMake(10, 15, 40, 40);
        self.iconImageView = [[UIImageView alloc] initWithFrame:rect];
        self.iconImageView.backgroundColor = [UIColor clearColor];
        self.iconImageView.layer.cornerRadius = 20.0f;
        self.iconImageView.layer.masksToBounds = YES;
        [backView addSubview:self.iconImageView];
        
        rect = CGRectMake(self.iconImageView.left, self.iconImageView.bottom+15, self.iconImageView.width, 25);
        self.deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
        self.deleteButton.frame = rect;
        self.deleteButton.backgroundColor = RGB(195, 81, 8);
        [self.deleteButton setTitle:@"删除" forState:UIControlStateNormal];
        [self.deleteButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.deleteButton.titleLabel.font = [UIFont systemFontOfSize:14.0f];
        [backView addSubview:self.deleteButton];
        
        rect.origin.x = backView.width - rect.origin.x - rect.size.width;
        self.shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
        self.shareButton.frame = rect;
        self.shareButton.backgroundColor = RGB(104, 202, 27);
        [self.shareButton setTitle:@"分享" forState:UIControlStateNormal];
        [self.shareButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.shareButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
        self.shareButton.titleLabel.font = [UIFont systemFontOfSize:14.0f];
        [backView addSubview:self.shareButton];
    
        self.photoButton = [UIButton buttonWithType:UIButtonTypeCustom];
        self.photoButton.frame = rect;
        self.photoButton.backgroundColor = RGB(104, 202, 27);
        self.photoButton.right = self.shareButton.left - 10;
        [self.photoButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.photoButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
        self.photoButton.titleLabel.font = [UIFont systemFontOfSize:14.0f];
        [backView addSubview:self.photoButton];
        
        CGFloat orginx = self.iconImageView.right + 10;
        rect = CGRectMake(orginx, 10, backView.width - orginx-10, 20);
        self.distanceLabel = [[UILabel alloc] initWithFrame:rect];
        self.distanceLabel.backgroundColor = [UIColor clearColor];
        self.distanceLabel.attributedText = [self createAttributedString];
        self.distanceLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        [backView addSubview:self.distanceLabel];
        
        UIButton *timeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        timeButton.frame = self.distanceLabel.frame;
        timeButton.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [timeButton addTarget:self action:@selector(onClickTimeButton:) forControlEvents:UIControlEventTouchUpInside];
        
        rect.origin.y = self.distanceLabel.bottom + 5;
        rect.size.height = 15.0f;
        self.calorieLabel = [[UILabel alloc] initWithFrame:rect];
        self.calorieLabel.backgroundColor = [UIColor clearColor];
        self.calorieLabel.textColor = RGB(201, 67, 72);
        self.calorieLabel.font = [UIFont systemFontOfSize:15.0f];
        self.calorieLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        self.calorieLabel.text = @"卡路里消耗0大卡";
        [backView addSubview:self.calorieLabel];
        
        rect.origin.y = self.calorieLabel.bottom + 5;
        rect.size.height = 12.0f;
        self.placeLabel = [[UILabel alloc] initWithFrame:rect];
        self.placeLabel.backgroundColor = [UIColor clearColor];
        self.placeLabel.textColor = [UIColor blackColor];
        self.placeLabel.font = [UIFont systemFontOfSize:13.0f];
        self.placeLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        [backView addSubview:self.placeLabel];
        
        [self.photoButton addTarget:self action:@selector(onClickPhotoButton:) forControlEvents:UIControlEventTouchUpInside];
        [self.shareButton addTarget:self action:@selector(onClickShareButton:) forControlEvents:UIControlEventTouchUpInside];
        [self.deleteButton addTarget:self action:@selector(onClickDeleteButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

- (void)drawCellWithPlanInfo:(PlanInfo *)planInfo
{
    self.planInfo = planInfo;
    
//    @autoreleasepool {
//        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//        dateFormatter.dateFormat = @"yyyy-MM-dd";
//        NSTimeZone *timeZone = [NSTimeZone localTimeZone];
//        NSDate *selectDate = [dateFormatter dateFromString:[NSString stringWithFormat:@"%d-%2d-%2d", planInfo.year, planInfo.month, planInfo.day]];
//        selectDate = [NSDate dateWithTimeInterval:[timeZone secondsFromGMT] sinceDate:selectDate];
//        
//        NSTimeInterval nowTimeInterval = floor([[NSDate date] timeIntervalSince1970]);
//        NSDate *tomorrowDate = [NSDate dateWithTimeIntervalSince1970:nowTimeInterval - ((unsigned long long)nowTimeInterval % 86400) + 86399];
//        
//        if ([selectDate compare:tomorrowDate] == NSOrderedDescending) {
//            self.photoButton.hidden = YES;
//            self.shareButton.hidden = YES;
//        }
//    }
    
    self.placeLabel.text = planInfo.address;
    
    self.distanceLabel.attributedText = [self createAttributedString];
    
    self.iconImageView.image = [UIImage imageNamed:planInfo.icon];
    
    [self updateCell];
}

- (void)updateCell
{
    if (self.planInfo.status == PlanInfoStatusNone) {
        self.photoButton.enabled = YES;
        [self.photoButton setTitle:@"开始" forState:UIControlStateNormal];
//        self.deleteButton.hidden = NO;
    }else if (self.planInfo.status == PlanInfoStatusStart) {
        self.photoButton.enabled = YES;
        [self.photoButton setTitle:@"结束" forState:UIControlStateNormal];
//        self.deleteButton.hidden = NO;
    }else if (self.planInfo.status == PlanInfoStatusStop){
        self.photoButton.enabled = YES;
        [self.photoButton setTitle:@"完成" forState:UIControlStateNormal];
//        self.deleteButton.hidden = NO;
    }else {
        self.photoButton.enabled = NO;
        [self.photoButton setTitle:@"拍照" forState:UIControlStateNormal];
//        self.deleteButton.hidden = YES;
    }
}

#pragma mark - Action
- (void)onClickPhotoButton:(id)sender
{
    if (self.planInfo.status == PlanInfoStatusEnd) {
        if ([self.delegate respondsToSelector:@selector(takePhotoWithPlanInfo:)]) {
            [self.delegate takePhotoWithPlanInfo:self.planInfo];
        }
    }else {
        if ([self.delegate respondsToSelector:@selector(runWithPlanInfo:)]) {
            [self.delegate runWithPlanInfo:self.planInfo];
        }
    }
}

- (void)onClickShareButton:(id)sender
{
    if ([self.delegate respondsToSelector:@selector(shareWithPlanInfo:)]) {
        [self.delegate shareWithPlanInfo:self.planInfo];
    }
}

- (void)onClickDeleteButton:(id)sender
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"确定删除吗?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    alert.tag = 10;
    [alert show];
}

- (void)onClickTimeButton:(id)sender
{
    if (self.planInfo.status == PlanInfoStatusStop) {
        if ([self.delegate respondsToSelector:@selector(selectTimeInCell:)]) {
            [self.delegate selectTimeInCell:self];
        }
    }
}

#pragma mark - private
- (NSAttributedString *)createAttributedString
{
    NSString *distance = [[NSString alloc] initWithFormat:@"%.1f",self.planInfo.distance];
    
    NSString *timeStr = nil;
    
    NSTimeInterval time = 0;
    if (self.planInfo.end_date && self.planInfo.start_date) {
        
        time = self.planInfo.end_date - self.planInfo.start_date;
        
    } else if (self.planInfo.start_date) {
        
        time = [[NSDate date] timeIntervalSince1970] - self.planInfo.start_date;
        
    }
    
    if (time > 86399) {
        time = 86399;
    }
    
    if (time > 3600) {
        timeStr = [NSString stringWithFormat:@"%02d:%02d:%02d", (NSInteger)time/3600, ((NSInteger)time%3600)/60, (NSInteger)time%60];
    } else {
        timeStr = [NSString stringWithFormat:@"%02d:%02d", ((NSInteger)time%3600)/60, (NSInteger)time%60];
    }
    
    if (timeStr.length == 0) {
        timeStr = @"00:00";
    }
    
    UIColor *textColor =RGB(201, 67, 72);
    
    NSDictionary *dict1 = @{NSFontAttributeName : [UIFont systemFontOfSize:14.0f],
                            NSForegroundColorAttributeName:textColor};
    NSAttributedString *attr1 = [[NSAttributedString alloc] initWithString:@"公里数 " attributes:dict1];
    
    NSDictionary *dict2 = @{NSFontAttributeName : [UIFont systemFontOfSize:20.0f],
                            NSForegroundColorAttributeName:textColor};
    NSAttributedString *attr2 = [[NSAttributedString alloc] initWithString:distance attributes:dict2];
    
    NSDictionary *dict3 = @{NSFontAttributeName : [UIFont systemFontOfSize:14.0f],
                            NSForegroundColorAttributeName:textColor};
    NSAttributedString *attr3 = [[NSAttributedString alloc] initWithString:@"  计时 " attributes:dict3];
    
    NSDictionary *dict4 = @{NSFontAttributeName : [UIFont systemFontOfSize:20.0f],
                            NSForegroundColorAttributeName:textColor};
    NSAttributedString *attr4 = [[NSAttributedString alloc] initWithString:timeStr attributes:dict4];
    
    NSMutableAttributedString *returnAttr = [[NSMutableAttributedString alloc] initWithAttributedString:attr1];
    [returnAttr appendAttributedString:attr2];
    [returnAttr appendAttributedString:attr3];
    [returnAttr appendAttributedString:attr4];
    
    return returnAttr;
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        if ([self.delegate respondsToSelector:@selector(deleteInfo:)]) {
            [self.delegate deleteInfo:self.planInfo];
        }
    }
}
@end
